package cn.lianwei.yonyong.controller;

import cn.lianwei.yonyong.dao.TestDao;
import cn.lianwei.yonyong.pojo.Kcxx;
import cn.lianwei.yonyong.pojo.Xscjb;
import cn.lianwei.yonyong.pojo.Xsjbxx;
import cn.lianwei.yonyong.service.impl.TestServiceImpl;
import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.net.URLEncoder;
import java.util.*;

@Controller
@RequestMapping("/test")
public class TestController {

    @Autowired
    private TestServiceImpl service;

    @Autowired
    private TestDao dao;

    //消息界面，查询所有消息展示在页面上
    @RequestMapping("/toIndex")
    public String toIndex(ModelMap model, @RequestParam(required = true,defaultValue = "1")Integer pageNo,
                          @RequestParam(required = false,defaultValue = "6")Integer pageSize){
    /*    List<Map> list=service.listXsInfo(null);

        //修改
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<Map> page=new PageInfo<Map>(list);
        model.put("page",page);

        //以上为修改内容
        model.put("list",list);*/
        return "info";
    }


    //新增修改课程
    @RequestMapping("/kc/saveKc")
    @ResponseBody
    public Map saveKc(Kcxx kcxx){

        if (kcxx == null) {
            throw new IllegalArgumentException("参数错误");
        }
        int result = 0;
        if (StringUtils.isEmpty(kcxx.getId())) {
            result = service.insertKc(kcxx);
        } else {
            result = service.updateKc(kcxx);
        }

        Map map = new HashMap();
        map.put("success", result > 0 );
        return map;
    }

    @RequestMapping("/kc/listKc")
    @ResponseBody
    public List listKc() {
        return service.listKc();
    }

    @RequestMapping("/kc/deleteKc")
    @ResponseBody
    public void deleteKc(String kcdm) {
        dao.deleteKc(kcdm);
        dao.deleteCj2(kcdm);
    }


    //新增修改成绩
    @RequestMapping("/cj/saveCj")
    @ResponseBody
    public Map saveCj(Xscjb xscjb){

        if (xscjb == null) {
            throw new IllegalArgumentException("参数错误");
        }
        int result = 0;
        if (StringUtils.isEmpty(xscjb.getId())) {
            result = service.insertCj(xscjb);
        } else {
            result = service.updateCj(xscjb);
        }

        Map map = new HashMap();
        map.put("success", result > 0 );
        return map;
    }

    @RequestMapping("/cj/listCj")
    @ResponseBody
    public List listCj() {
        return service.listCj();
    }

    @RequestMapping("/cj/deleteCj")
    @ResponseBody
    public void deleteCj(String cjid) {
        service.deleteCj(cjid);
    }

    @GetMapping("xsExport")
    public void qyAptExort(HttpServletRequest request, HttpServletResponse response) throws Exception {

        Map map = getDynamicKcMap(null);
        List<List<String>> headerList = (List<List<String>>) map.get("headerList");
        List<List<Object>> dataList = (List<List<Object>>) map.get("dataList");


        String fileName = "讯百测试导出.xlsx";
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        EasyExcel.write(response.getOutputStream(), Xsjbxx.class)
                .autoCloseStream(Boolean.TRUE)
                .sheet("sheet1")
                .head(headerList)
                .doWrite(dataList)
                ;
    }

    @RequestMapping("/getDynamicKcMap")
    @ResponseBody
    public Map getDynamicKcMap(@Param("keyword") String keyword) {
        List<Kcxx> kcxxes = dao.listKc();

        List<List<String>> headerList = new ArrayList<>();
        headerList.add(Arrays.asList("学号"));
        headerList.add(Arrays.asList("姓名"));
        headerList.add(Arrays.asList("年级"));
        headerList.add(Arrays.asList("班级"));

        for (Kcxx kc : kcxxes) {
            headerList.add(Arrays.asList(kc.getKcmc()));
        }
        headerList.add(Arrays.asList("平均分"));

        Map map = new HashMap();
        map.put("kcList", kcxxes);
        map.put("keyword", keyword);
        List<Map> list = dao.listXsInfo(map);

        List<List<Object>> dataList = new ArrayList<>();
        for (Map<String, Object> row : list) {
            List<Object> oneRow = new ArrayList<>();
            oneRow.add(row.get("xh"));
            oneRow.add(row.get("xm"));
            oneRow.add(row.get("nj"));
            oneRow.add(row.get("bj"));
            for (Kcxx kc : kcxxes) {
                String colName = kc.getKcdm();
                oneRow.add(row.get(colName));
            }
            oneRow.add(row.get("avgCj"));
            dataList.add(oneRow);
        }

        Map resultMap = new HashMap();
        resultMap.put("headerList", headerList);
        resultMap.put("dataList", dataList);
        return resultMap;
    }


}
